package com.briup.srms.sms.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.srms.sms.bean.GraduateApply;
import com.briup.srms.sms.bean.StudentEmployment;
import com.briup.srms.sms.bean.extend.GraduateApplyExtend;
import com.briup.srms.sms.dao.GraduateApplyMapper;
import com.briup.srms.sms.dao.extend.GraduateApplyExtendMapper;
import com.briup.srms.sms.dao.extend.StudentEmploymentExtendMapper;
import com.briup.srms.sms.service.IGraduateApplyService;
import com.jacky.base.utils.CustomerException;
import com.jacky.base.utils.PageVM;

@Service
public class GraduateApplyServiceImpl implements IGraduateApplyService {
	
	@Autowired
	private GraduateApplyExtendMapper graduateApplyExtendMapper;
	@Autowired
	private GraduateApplyMapper graduateApplyMapper;
	@Autowired
	private StudentEmploymentExtendMapper seeMapper;

	@Override
	public PageVM<GraduateApplyExtend> pageQuery(int page, int pageSize,Long studentId) {
		List<GraduateApplyExtend> list = graduateApplyExtendMapper.query(page, pageSize,studentId);
		long total = graduateApplyExtendMapper.count(studentId);
		return new PageVM<>(page, pageSize, total, list);
	}

	@Override
	public void saveOrUpdate(GraduateApply apply) throws CustomerException {
		// TODO Auto-generated method stub
		if (apply.getId() != null) {
			graduateApplyMapper.updateByPrimaryKey(apply);
		} else {
			
			// 初始化
			apply.setApplyTime(new Date().getTime());
			
			graduateApplyMapper.insert(apply);
		}
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		graduateApplyMapper.deleteByPrimaryKey(id);
	}
	
	@Override
	public void check(Long id, boolean isPass) {
		// 审核
		GraduateApply apply = graduateApplyMapper.selectByPrimaryKey(id);
		if(apply== null) {
			throw new CustomerException("离校申请不存在");
		}
		if(isPass) {
			apply.setCheckStatus("通过");
			// 查询到学生就业信息
			StudentEmployment employment = seeMapper.queryByStudentId(apply.getStudentId());

		} else {
			apply.setCheckStatus("驳回");
		}
		
		// 更新
		graduateApplyMapper.updateByPrimaryKey(apply);
	}

}
